﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TradingApp.Admin;
using TradingApp.Helper;
using TradingAppDal;
using TradingApp.Data;

namespace TradingApp
{
    public partial class frmTradingAppParent : Form
    {
        public frmTradingAppParent()
        {
            InitializeComponent();
        }
        frmMainApp ofrmMainApp;
        frmReporting ofrmReporting = new frmReporting();
        public frmMainApp MainAppInstance
        {
            get
            {
                return ofrmMainApp;
            }
        }
        frmAdminSettings ofrmAdmin  = new frmAdminSettings(); 
        private void applicationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (ofrmMainApp == null)
                {
                    ofrmMainApp = new frmMainApp();
                    OpenFormAsMdiChild(ofrmMainApp);
                }
                else
                {
                    //ofrmMainApp.Show();
                    ofrmMainApp.Select();
                    return;
                }

            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }
        public frmReporting GetReportingScreen()
        {
            return ofrmReporting;
        }
        private void miDatabaseConnection_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFormAsMdiChild(ofrmAdmin);
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
            //try
            //{
                
            //    ofrmConnectionString.ShowDialog();
            //}
            //catch (System.Exception ex)
            //{
            //    UIHelper.HandleApplicationException(ex);
            //}
        }

        private void miPoolingInterval_Click(object sender, EventArgs e)
        {
            try
            {
                frmPoolInterval ofrmPoolInterval = new frmPoolInterval();
                ofrmPoolInterval.Tag = this;
                ofrmPoolInterval.ShowDialog();
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void frmTradingAppParent_Load(object sender, EventArgs e)
        {
            try
            {
                //LoadAnalysisScreenTemp();
                LoadAnalysisScreen();
                //LoadReportingScreen();
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void LoadReportingScreen()
        {
            TestDB();
            frmReporting ofrmReporting = new frmReporting();
            OpenFormAsMdiChild(ofrmReporting);
        }

        private void LoadAnalysisScreen()
        {
            frmLogin ofrmLogin = new frmLogin();

            if (ofrmLogin.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //passed login - can load data

                TradingAppSettings.PoolIntervalInSecs = XmlHelper.GetIntTagValue(TradingAppSettings.TIMEOUT_ATTRIBUTE_STR);
                TradingAppSettings.DBConnectionStr = XmlHelper.GetStringTagValue(TradingAppSettings.CONN_ATTRIBUTE_STR);
                TradingAppSettings.MaxThreadCount = XmlHelper.GetIntTagValue(TradingAppSettings.MAXTHREAD_ATTRIBUTE_STR);
                TradingAppSettings.DefaultReport = XmlHelper.GetStringTagValue(TradingAppSettings.DEFAULTREPORT_ATTRIBUTE_STR);
                while (String.IsNullOrEmpty(TradingAppSettings.DBConnectionStr) || !AdminDal.TestConnectionString(TradingAppSettings.DBConnectionStr))
                {
                    frmAdminSettings ofrmConnectionString = new frmAdminSettings();
                    ofrmConnectionString.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
                    ofrmConnectionString.ShowDialog();
                }
                //TestDB();
                ofrmMainApp = new frmMainApp();
                OpenFormAsMdiChild(ofrmMainApp);
            }
            else
            {
                MessageBox.Show("Login failed.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void LoadAnalysisScreenTemp()
        {
            
                TestDB();
                ofrmMainApp = new frmMainApp();
                OpenFormAsMdiChild(ofrmMainApp);
            
        }

        private void TestDB()
        {
            TradingAppSettings.DBConnectionStr = @"Data Source=USER-PC\SQL2008R2;User Id=sa;password=ampere;Initial Catalog=WC;";
            TradingAppSettings.PoolIntervalInSecs = 5;
            TradingAppSettings.MaxThreadCount = 1;
            TradingAppSettings.DefaultReport = XmlHelper.GetStringTagValue(TradingAppSettings.DEFAULTREPORT_ATTRIBUTE_STR);
            //EntityDal oEntityDal = new EntityDal();
            //oEntityDal.GetEntities(EntityType.ToProcess);
            //oEntityDal.UpdateEntity(3, EntityType.InQueue);

            //SecurityDal oSecurityDal = new SecurityDal();
            //oSecurityDal.GetSecurities(4, EntityType.ToProcess);
            //oSecurityDal.UpdateSecurity(1, EntityType.InQueue);
        }

        #region Methods

        // UI

        private void OpenFormAsMdiChild(Form form)
        {
            UIHelper.OpenFormAsMdiChild(form, this, FormWindowState.Normal);
        }

        private void OpenFormAsMdiChild(Form form, FormWindowState windowState)
        {
            UIHelper.OpenFormAsMdiChild(form, this, windowState);
        }

        #endregion

        private void reportingTSMI_Click(object sender, EventArgs e)
        {
            try
            {
                ofrmReporting .SetCustomLoad(false,null,null,false);
                OpenReportingScreen();
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }

        }

        public void OpenReportingScreen()
        {
            OpenFormAsMdiChild(ofrmReporting);
        }

        private void analysisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (ofrmMainApp == null)
                {
                    ofrmMainApp = new frmMainApp();
                }
                OpenFormAsMdiChild(ofrmMainApp);

            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }
    }
}
